package com.caipiao.lottery.task.keepline.danmu;

import com.caipiao.util.IdMaker;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.util.ArrayList;
import java.util.Set;

public class HuYaTv extends Thread{

    private ArrayList<String> arr = new ArrayList<String>();

    public static void main(String[] args) throws Exception{
        String str186 = "webps=A;__FEQUALITY__UUID=0cf5a34a-5eeb-42ca-a026-f3e8b10b7b31;"+
                "PHPSESSID=me7i0ntngeqaf1vol6s8h3rkt5;__yasmid=0.2326809955142517;__yamid_tt1=0.2326809955142517;"+
                "__yamid_new=C74D96451BA00001DBE31B017D2035B0;hiido_ui=0.5484700984040964;hd_newui=0.04930049683471727;"+
                "hdjs_session_id=0.8247199596338688;udboauthtmptoken=d9eab51c1e1590d68e902620a03e7e743b6fa958861f9a6d55812"+
                "4f122169c3603300cab5b0fda67c7046496c4d31222b217500274e367dced78c4735d626c22;udboauthtmptokensec=;"+
                "h_pr=1;yyuid=1648021359;username=1870714869yy;password=184D2D406094A90225C238510E8E4C856328BE77;"+
                "osinfo=E04ED6AA1210D9584FAA46237D9810352E135B12;udb_l=DAAxODcwNzE0ODY5eXkjPS1YBnQA3iQ0m84mzzp4J_1cFc3n_83h7yo4Xfi"+
                "B-pRTU_iRemC0S14DmvzboFK7Tx0QmaK2gJu1oG9G1qqug_wkB0OR-OHoZrqLuan1jIbgKiHgDEJGPyeTlUpxmenifp7dY6EYSOlTbg5l1sbWJiF"+
                "2tM0JEdDbFDwAAAAAAwAAAAAAAAALADYwLjIxNi4zLjM0BAA1MjE2;udb_n=4ded8f4f46beec5bbd310d53f9d5596b19fa154113638886319"+
                "89b3132413cdcfbd2833e5b04dcd5b8c02e70655bf22e;udb_c=AMARBlBqAAJgACoWQI3OAy_7eEs2xhRnAqA-GME4Xq8cJGRtxvyCpJddgOh"+
                "8WiMf6GWEQ3sU5X-33f4qpliSya1fub9obmoTdk5Ut_lSnkA310TYkiNXiB1U8nsnldDf04KCst5Jf1WoHA==;udb_oar=1035F43D6E25208F8"+
                "8F6CB1263966FE936A4C987B95DDCF0665066A6936642EBF0C72AD48B4BE97C2FCB092E68EEB7B54162AFB89BC10930E350A63DC7C0468B0"+
                "76C235B57CF70074300CE008632665246C6B888F1D1FCA84D8650A98260F0DE7B40660AB7F2C167BF785A0C1641A27EB786B9C307F4FCC89F"+
                "E266530ECBA3A58176C0DF58192F3D9F08AE25CBC7EC763B8E02C9221EED037CD7316D4C058F435255C04642610B071AF441028E4D6AEDE73"+
                "0FD5D30E48C2B5EF9B63D4FCFC33C5F872BDFBD4562479728AFC1ECDC3C1302DF504FCF8D2310C5A44507B6A2E717E128CDED6F23F5A092E2"+
                "E4C5352CCAEDC59EBB3F52DAF7B6675E23B1C4A0C1A138C8308E07EFA995F0268BCDD9FD7453A68550010FC5E20F94793C3EE86A5B89C596"+
                "6BBA1FBD19863BF6A7753D1BB2ADA56020AC490360E08BCCEFBAFCBEFE3F;h_unt=1479359781;__yaoldyyuid=1648021359;_yasids"+
                "=__rootsid%3DC74D9649C01000012B852FB019211714;ya_eid=home/remenup/1421992885;isInLiveRoom=true;SoundValue=0.50;"+
                "guid=b7389206153d2d58f574504851487ec3;Hm_lvt_51700b6c722f5bb4cf39906a596ea41f=1479359770;Hm_lpvt_51700b6c722f5bb"+
                "4cf39906a596ea41f=1479360514;hdjs_session_time=1479360513613";

        String str152 = "webps=A;PHPSESSID=4acrjf21jt6k71l6c4pg43tug5;__FEQUALITY__UUID=48804166-ba3b-4861-ba01-75da3d62b93d;__yasmid="+
                "0.6281068937589356;__yamid_tt1=0.6281068937589356;__yamid_new=C74D987A15400001A17963B01DBB5870;hiido_ui=0.127346029483"+
                "11946;hd_newui=0.5123630724864259;hdjs_session_id=0.19886638908790655;ya_eid=navi/login;udboauthtmptoken=981f372a6859"+
                "c12db810ce109d018226574ae98704683db841d6db35f0d3e611788c2525f69d7a99cfd1ae3da2b776a6;udboauthtmptokensec=;h_pr=1;yyuid="+
                "1616337173;username=1830699362yy;password=BF1461C7BCDB3B637F4B0ACC85FE83B1640F0745;osinfo=F7D4BA8D7AC156F31E53D26E80B34"+
                "F67F80899ED;udb_l=DAAxODMwNjk5MzYyeXn1Ri1YA3QAyUzF_y4Q94ZNzEei4wML-OybJlF5315Dxhc1OyMbb_ynJoRXsqrQN2s7HKj9R8GlZ1cptFvbBkt"+
                "i5xo5eGSuOat3j6xfWLqhKgmkndicAH6XDiHp_J341VLCzzYpKE9rhS6p3k2nul3K0a8zep_24l3-qwsAAAAAAwAAAAAAAAALADYwLjIxNi4zLjM0BAA1MjE2;"+
                "udb_n=318c0326b342223f95e7c0f616e8841822e73288a795771bb7bc8348b093e883a25cab755c14cc3ff2a64f598ba83849;udb_c=AMAaBlBqAAJ"+
                "gAE27gNHsY61Y2ddgK6XwaZd40GrhznnxYQ6n4ZghEuI4kl1PHIoQGN4dYTclVFBAtxgI1CJLx2Gqj5WtahuD9GzGyI9l-_qnuTj8UL6c00yBA8Vg2SzCOgslKI"+
                "Nx85BLfQ==;udb_oar=201E91B6604793996B239A55AFB8BBEE40CE0EB1F98C7F5BF89D617C73DB085C20AFB3D73C9A122862D64759EB35A9F2D3EFE39"+
                "3F47BC72AA6ADD880076B900A04F5497F126541ED3BFDA4D356D866B135C414C103503CB2780F289CE2B61A23945A20D7478B467C8AAC949AD6AB946962"+
                "12F85A4263832558244EB0BF4D9E5F1E08EDC9072D160C22A7D13BC8199626787078C5D81D68D6DB2BE007A2ED2D357C9161B1F977A2F666D7DF74E1A14"+
                "340DD3962439E3521839DF05B764F89ACBD4EFB91890465622973688994992C13DC2D8BD056109656CA521435F7B2DB79F07437CC4050E9891D20C279E0"+
                "5835FE0EF32D37FF9EA9CD6533C5CE37C32DE31879B80021E948858C282FD59717FF369F0CA7C576A555AFEAEEFB614BFF6DA3DA21E8701D00192FDAF8F"+
                "D9C5D44D76E75986280CE52DE48D945380798EFA209B4;isInLiveRoom=true;h_unt=1479362296;SoundValue=0.50;guid=b738920620462d58f"+
                "274bacbbbcb625b;__yaoldyyuid=1616337173;_yasids=__rootsid%3DC74D98AF62C00001B59A1F75AF50144C;Hm_lvt_51700b6c722f5bb4cf39"+
                "906a596ea41f=1479359770;Hm_lpvt_51700b6c722f5bb4cf39906a596ea41f=1479362303;hdjs_session_time=1479362303250";

        System.out.println(str186);

        Thread thread186 = new HuYaTv(str186);
//
//        Thread thread152 = new HuYaTv(str152);
//
        CookieUtile.flag = false;
//
        thread186.start();
//        Thread.sleep(60000);
//        System.out.println("准备停止");
//        Thread.sleep(1000);
//        Data.flag = true;
//        thread152.start();
    }

    public HuYaTv(String cookieStr){
        cookieStr = cookieStr.replaceAll(" ", "");
        String[] cos = cookieStr.split(";");
        for(String co: cos){
            arr.add(co);
        }
    }



    public void add_cookies(WebDriver driver) {
        for (String kv : arr) {
            Cookie cookie = new Cookie(kv.split("=")[0], kv.split("=")[1]);
            driver.manage().addCookie(cookie);
        }
// And now output all the available cookies for the current URL
        if (false) {
            Set<Cookie> allCookies = driver.manage().getCookies();
            for (Cookie loadedCookie : allCookies) {
                System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));


// You can delete cookies in 3 ways
// By name
                driver.manage().deleteCookieNamed("CookieName");
// By Cookie
                driver.manage().deleteCookie(loadedCookie);
// Or all of them
            }
            driver.manage().deleteAllCookies();
        }
    }

    @Override
    public void run() {
        try {
            // Create a new instance of the Chrome driver
            // Notice that the remainder of the code relies on the interface,
            // not the implementation.
            File file = new File("D:\\YunPan\\WorkCode\\idea_workspace\\AndroidSimulatorTestHttpPost1\\chromedriver.exe");
            System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());

            WebDriver driver = new ChromeDriver();

            String url = "http://www.huya.com/1547946820";
            driver.get(url);
            for (String kv : arr) {
                System.out.println("String kv===>>" + kv);
                Cookie cookie = new Cookie(kv.split("=")[0], kv.split("=", -1)[1]);
                driver.manage().addCookie(cookie);
            }
            driver.navigate().refresh();

            for (int i = 0; i < 20; i++) {
                System.out.println("Data.flag="+CookieUtile.flag);
                if(CookieUtile.flag){
                    System.out.println("执行完毕，退出");
                    break;
                }
                System.out.println("执行");
                Thread.sleep(7000);
                WebElement element = driver.findElement(By.id("pub_msg_input"));
                //                element.sendKeys("A用户" + Rand.getRandomNumString(rand_time_interval));
                //获取人气值
                WebElement renqi = driver.findElement(By.xpath("//em[@id='live-count']"));
                System.out.println("=======>"+renqi.getText());
                for (int j = 0; j < Math.round(Math.random()*10); j++) {
                    element.sendKeys("6");
                }
                WebElement element2 = driver.findElement(By.id("msg_send_bt"));
                element2.click();
            }

            driver.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}